/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.aptech.dao;

import com.aptech.beans.Suppliers;
import com.aptech.utility.DBType;
import com.aptech.utility.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author Anh Nguyen
 */
public class SupplierTypeManager {
    public static ResultSet displayAllRows() throws Exception{
        String sql = "SELECT * FROM SupplierTypes";
        Connection conn = DBUtil.getConnection(DBType.myConnection);
        PreparedStatement preparedStatement = conn.prepareStatement(sql,
            ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        ResultSet resultSet = preparedStatement.executeQuery();
        return resultSet;
    }
    
    public static Suppliers checkName(String SupplierTypeName) throws Exception{
        String sql = "SELECT * FROM SupplierTypes WHERE SupplierTypeName = ?";
        Connection conn = DBUtil.getConnection(DBType.myConnection);
        PreparedStatement preparedStatement = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        preparedStatement.setString(1, SupplierTypeName);
        ResultSet resultSet = preparedStatement.executeQuery();
        if (resultSet.next()) {
            Suppliers supplier = new Suppliers();
            supplier.setSupplierName(SupplierTypeName);
            return supplier;
        } else {
            return null;
        }
    }
    
    public static boolean insert(Suppliers supplierType) throws Exception{
        String sql = "INSERT INTO SupplierTypes (SupplierTypeName, Description) VALUES (?, ?)";
        Connection conn = DBUtil.getConnection(DBType.myConnection);
        conn.setAutoCommit(false);
        
        PreparedStatement preparedStatement = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        preparedStatement.setString(1, supplierType.getSupplierTypeName());
        preparedStatement.setString(2, supplierType.getDescription());
        int affected = preparedStatement.executeUpdate();
        conn.commit(); //transaction block end
        return affected == 1;
    }
    
    
    public static boolean update(Suppliers supplierType) throws Exception{
        String sql = "UPDATE SupplierTypes SET SupplierTypeName=?, Description=? WHERE SupplierTypeID=?";
        Connection conn = DBUtil.getConnection(DBType.myConnection);
        conn.setAutoCommit(false);
        
        PreparedStatement preparedStatement = conn.prepareStatement(sql,
                ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        
        preparedStatement.setString(1, supplierType.getSupplierTypeName());
        preparedStatement.setString(2, supplierType.getDescription());
        preparedStatement.setInt(3, supplierType.getSupplierTypeID());
        int affected = preparedStatement.executeUpdate();
        conn.commit();
        return affected == 1;
    }
    
}
